"use strict";
//定义变量用于抓取网站
var
    system = require('system'),
    ScrapUrlToFile;


ScrapUrlToFile = function (callbackFinal, url, path, body) {
    var
        next,
        page = null,
        retrieve,
        webpage = require("webpage"),
        fs = require('fs');
    /*
     等待函数，用来等待直到 testFx 返回 true，才会执行 onReady，可以用来面对比较恶心的网站
     */
    var waitFor = function (testFx, onReady, timeOutMillis) {
        var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 30000, //< Default Max Timout is 30s
            start = new Date().getTime(),
            condition = false,
            interval = setInterval(function () {
                if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
                    // If not time-out yet and condition not yet fulfilled
                    condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
                } else {
                    if (!condition) {
                        // If condition still not fulfilled (timeout but condition is 'false')
                        console.log("'waitFor()' timeout");
                        phantom.exit(1);
                    } else {
                        // Condition fulfilled (timeout and/or condition is 'true')
                        console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                        typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                        clearInterval(interval); //< Stop this interval
                    }
                }
            }, 250); //< repeat check every 250ms
    };
    next = function (status, url, file) {
        page.close();
        typeof(callbackFinal) === "string" ? eval(callbackFinal) : callbackFinal(status, url, file);
    };
    retrieve = function () {

        page = webpage.create();
        page.viewportSize = {width: 414, height: 736,margin:0};
        page.onConsoleMessage = function (msg) {
            console.log(msg);
            printArgs.apply(this, arguments);
        };
        page.settings.userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1";
        page.onLoadFinished = function () {
            console.log("page.onLoadFinished");
            printArgs.apply(this, arguments);
        };
        return page.open(address, function (status) {
            var start = Date.now();
            if (status !== 'success') {
                console.log('FAIL to load the address');
                phantom.exit(1);
            } else {
                /*
                 打印一下网站标题，用来确认正确的访问网站了
                 */
                var t = Date.now() - start;
                console.log('Loading time ' + t / 1000 + ' sec');
                var title = page.evaluate(function () {
                    return document.title;
                });
                var favicon = page.evaluate(function () {
                    var link = document.querySelector("link[rel*='shortcut icon']");
                    if (link)
                        return link.href;
                    else {
                        var url = location.protocol + "//" + location.host;
                        if (location.port)
                            url += ":" + location.port;
                        return url + "/favicon.ico"
                    }
                });
                console.log(title);
                var jsonBody={"title":title,"favicon":favicon};
                fs.write(path + '/title.html', JSON.stringify(jsonBody), 'w');
                var
                    arrayOfUrls = [];
                arrayOfUrls = page.evaluate(function (arrayOfUrls) {
                    /*模拟窗口滚动*/
                    var simulateScroll = function (simulateScrollCallback) {
                        console.log("start to caculate height")
                        var body = document.body,
                            html = document.documentElement;
                        var height = Math.max(body.scrollHeight, body.offsetHeight,
                            html.clientHeight, html.scrollHeight, html.offsetHeight);
                        console.log("height is " + height);

                        var scrollAndWait = function (h, height) {
                            console.log("start to scrollAndWait")
                            console.log("scroll from " + h)
                            h = h + window.innerHeight;
                            console.log("scroll to " + h);
                            window.scroll(0, h);
                            if (h < height) {
                                var start = Date.now();
                                for (; (Date.now() - start) < 2000;);
                                scrollAndWait(h, height);
                            }
                            else if (typeof simulateScrollCallback === "function")
                                simulateScrollCallback();
                        };
                        scrollAndWait(0, height);
                    };
                    simulateScroll();
                    var getFilename = function (imgPath) {
                        var imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1, imgPath.length);
                        if (imgName.indexOf(".") === -1) {
                            imgName = imgPath.substring(imgPath.lastIndexOf("/", imgPath.lastIndexOf("/") - 1) + 1, imgPath.lastIndexOf("/")) + ".png";
                        }
                        if (imgName.length > 30)
                            imgName = imgName.substring(imgName.length - 30, imgName.length);
                        return imgName;
                    };
                    var start = Date.now();
                    var images = document.getElementsByTagName("img");
                    console.log("images size is :" + images.length);
                    for (var i = 0; i < images.length; i++) {
                        if (images[i].getAttribute("src") == undefined || images[i].getAttribute("src").indexOf("image/gif;base64") > 0) {
                            console.log("Img origin is " + i + ": " + images[i].getAttribute("src"));
                            images[i].setAttribute("src", images[i].getAttribute("data-src"));
                        }
                        console.log("Img now is " + i + ": " + images[i].getAttribute("src"));
                        /*
                         下载图片到本地并且修改访问连接
                         */
                        var imgPath = images[i].getAttribute("src");
                        var imgName = getFilename(imgPath);
                        images[i].setAttribute("source-src", imgPath);
                        images[i].setAttribute("id", "image-load-" + i);
                        var data = {
                            "url": imgPath,
                            "imgName": imgName,
                            "id": "image-load-" + i,
                            "position": {
                                top: images[i].offsetTop,
                                left: images[i].offsetLeft,
                                width: images[i].offsetWidth,
                                height: images[i].offsetHeight
                            }
                        };
                        arrayOfUrls.push(data);
                        var t = Date.now() - start;
                        console.log('Loading time ' + t / 1000 + ' sec');
                    }
                    return arrayOfUrls;
                }, arrayOfUrls);
                /*去掉所有script标签*/
                page.evaluate(function () {
                    while (document.getElementsByTagName("script").length > 0)
                        document.getElementsByTagName("script")[0].outerHTML = "";
                });


                // Wait for 'signin-dropdown' to be visible
                waitFor(function () {
                    // Check in the page if a specific element is now visible
                    return page.evaluate(function () {
                        var images = document.getElementsByTagName("img");
                        for (var i = 0; i < images.length; i++) {
                            if (images[i].getAttribute("src").indexOf("image/gif;base64") > 0) {
                                console.log("Img " + i + " is " + images[i].getAttribute("src"));
                                return false;
                            }
                        }
                        return true;
                    });
                }, function () {
                    console.log("WaitFor end.");
                    /*
                     写入文件的逻辑
                     */
                    try {
                        console.log("path = " + path);

                        page.render(path + '/index.png');
                        arrayOfUrls.forEach(function (imageObj, index, array) {
                            if (imageObj.imgName.length > 4) {
                                console.log("downloading " + imageObj.url + "to " + imageObj.imgName);
                                page.clipRect = imageObj.position;
                                page.render(path + "/" + imageObj.imgName);
                            }
                        });
                        arrayOfUrls.forEach(function (imageObj, index, array) {
                            if (imageObj.imgName.length > 4) {
                                console.log("imageObj.id " + imageObj.id + " to " + imageObj.imgName);
                                page.evaluate(function (imageObj) {
                                    var dom = document.getElementById(imageObj.id);
                                    console.log("dom origin is " + dom.outerHTML);
                                    if (dom !== undefined) {
                                        dom.setAttribute("src", "./" + imageObj.imgName)
                                    }
                                    console.log("dom now is " + dom.outerHTML);
                                }, imageObj);
                            }
                        });
                        /*增加广告*/
                        console.log("body = " + body);
                        if (body)
                            page.evaluate(function (body) {
                                document.body.innerHTML = document.body.innerHTML + body;
                            }, body);
                        /*获取文章正文*/
                        var content = page.evaluate(function () {
                            console.log("Start download body");
                            content = document.documentElement.outerHTML;
                            console.log(content);
                            console.log("End download body");
                            return content;
                        });
                        fs.write(path + '/index.html', content, 'w');
                    } catch (e) {
                        console.log(e);
                    }

                    next(status, url, path + '/index.html');
                });
            }
        });
    };
    return retrieve();
};


console.log("\n" +
    "____    __    ____  _______  __    _______      ___       ______   \n" +
    "\\   \\  /  \\  /   / |   ____||  |  /  _____|    /   \\     /  __  \\  \n" +
    " \\   \\/    \\/   /  |  |__   |  | |  |  __     /  ^  \\   |  |  |  | \n" +
    "  \\            /   |   __|  |  | |  | |_ |   /  /_\\  \\  |  |  |  | \n" +
    "   \\    /\\    /    |  |____ |  | |  |__| |  /  _____  \\ |  `--'  | \n" +
    "    \\__/  \\__/     |_______||__|  \\______| /__/     \\__\\ \\______/  \n" +
    "                                                                   ");
var address, path, body;
console.log("system.args.length = " + system.args.length);
for (var i = 0; i < system.args.length; i++) {
    console.log('arg = ' + system.args[i]);
}
if (system.args.length < 3) {
    console.log('Usage: scrapToDirectory.js <some URL> <targetDirectory Directory> [body body]');
    phantom.exit(1);
} else if (system.args.length === 3) {
    console.log('system.args.length = 3');
    address = system.args[1];
    path = system.args[2];
    ScrapUrlToFile((function (status, url, file) {
        if (status !== "success") {
            console.log("Unable to render '" + url + "'");
        } else {
            console.log("Rendered '" + url + "' at '" + file + "'");
            phantom.exit();
        }
    }), address, path);
} else if (system.args.length === 4) {
    console.log('system.args.length = 4');
    address = system.args[1];
    path = system.args[2];
    body = system.args[3];
    ScrapUrlToFile((function (status, url, file) {
        if (status !== "success") {
            console.log("Unable to render '" + url + "'");
        } else {
            console.log("Rendered '" + url + "' at '" + file + "'");
            phantom.exit();
        }
    }), address, path, body);
}